// 引入模块
var express = require('express');
var path = require('path');
var cors = require('cors');

// 引入路由模块
var indexRouter = require('./routes/index');
var onlineRouter = require('./routes/online');
var searchRouter = require('./routes/search');
var detailRouter = require('./routes/detail');
var registerRouter = require('./routes/register');
var loginRouter = require('./routes/login');
var userRouter = require('./routes/user');


// 创建服务器
var app = express();

// 设置模板引擎
app.set('views', path.join(__dirname, 'views'));//设置模板的文件夹
app.set('view engine', 'ejs');


//注册中间件
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// ------------------------
app.use("/public",express.static(path.join(__dirname, 'public')));
app.use("/uploads",express.static(path.join(__dirname, 'uploads')));
// ----------------------------
app.use(cors())

// 注册路由中间件
app.use('/', indexRouter);
app.use('/', onlineRouter)
app.use('/', searchRouter)
app.use('/', detailRouter)
app.use('/', registerRouter)
app.use('/', loginRouter)
app.use('/', userRouter)


// 404
app.use(function(req, res, next) {
    res.send({
      status:404,
      msg:"此页面不存在"
    })
});


//服务器错误
app.use(function(err, req, res, next) {
    res.send({
      status:500,
      msg: err || "服务器崩溃了~~~"
    })
});

module.exports = app;
